//
//  CJMenuController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-06-01.
//

import UIKit


class CJMenuTextItem: CJMenuItem {
    
    let titleL = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupView()
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupView() {
        backgroundColor = .clear
        titleL.font = .systemFont(ofSize: 13, weight: .regular)
        titleL.textAlignment = .center
        self.addSubview(self.titleL)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        titleL.frame = CGRect(x: 0, y: 0, width: cj_width, height: cj_height)
    }
    
    override var data: Any? {
        didSet {
            guard let data = data as? String else { return }
            titleL.text = data
            titleL.sizeToFit()
            
            var rect = titleL.frame
            rect.size.width = rect.size.width + 5
            self.frame = rect
        }
    }
}

class CJMenuCell: UICollectionViewCell {
    
}

class CJMenuController: CJBaseBackController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "菜单视图"
        setupDirection()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "切换", style: .done, target: self, action: #selector(setupDirection))
    }
    
    var direction: Bool = false
    var menuView: CJMenuView!
    var collectionView: UICollectionView!
    
    @objc func setupDirection() {
        
        for sub in self.view.subviews {
            sub.removeFromSuperview()
        }
        direction = !direction
        setupView()
        setupData()
    }
    
    func setupData() {
        let datas = ["标", "标题", "标题标题", "标题标题标题", "标题标题标题标题", "标题", "标题标题", "标题标题标题", "标题", "标题标题标题", "标题", "标题标题", "标题标题标题标题", "标题"]
        menuView.datas = datas
        menuView.reloadData()
        menuView.setupCurrent(value: 1, fromIndex: 0, toIndex: 0)
    }
    
    func setupView() {
        
        let config = CJMenuConfig()
        if direction {
            menuView = CJMenuView(frame: CGRect(x: 0, y: CJScreen.navBatBarHeight, width: view.cj_width, height: 40))
            config.direction = .horizontal
        } else {
            menuView = CJMenuView(frame: CGRect(x: 0, y: CJScreen.navBatBarHeight, width: 90, height: view.cj_height - CJScreen.navBatBarHeight))
            config.direction = .vertical
            config.isAverageItem = true
        }
        self.view.addSubview(menuView)
        menuView.config = config
        menuView.didItemData = { menuView, index in
            let item = CJMenuTextItem()
            return item
        }
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRect(x: 100, y: 200, width: view.cj_width - 100, height: view.cj_height - 400), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        view.addSubview(collectionView)
        
        collectionView.register(CJMenuCell.self, forCellWithReuseIdentifier: "CJMenuCell")
    }
    
}

extension CJMenuController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == collectionView {
            let offsetx = scrollView.contentOffset.x
            menuView.setupCurrent(offset: offsetx, width: scrollView.cj_width, count: menuView.datas.count)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menuView.datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CJMenuCell", for: indexPath)
        cell.backgroundColor = .cj_colorRandom
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.cj_width, height: collectionView.cj_height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = PMenuController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}
